﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.ConfigManager;
using System.Security.Cryptography;

namespace ScheduleMaker
{
    public partial class smLoginForm : Form
    {
        public smLoginForm()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            try
            {
                ConfigManager.ConfigManager.loadConfiguration(tbPass.Text);
                if (ConfigManager.ConfigManager.isConfigLoaded())
                {
                    if (Authenticator.autenticate(tbLogin.Text, tbPass.Text))
                    {
                        string level = Authenticator.Level;
                        if (level.Equals("ADMIN"))
                        {
                            Form smAdmin = new smAdminForm();
                            smAdmin.FormClosed += new FormClosedEventHandler(main_FormClosed);
                            smAdmin.Show();
                            this.Hide();
                        }
                        else if (level.Equals("TEACHER"))
                        {
                            Form smTeacher = new smTeacherForm();
                            smTeacher.FormClosed += new FormClosedEventHandler(main_FormClosed);
                            smTeacher.Show();
                            this.Hide();
                        }
                        else if (level.Equals("NORMAL_USER"))
                        {
                            Form smStudent = new smStudentForm();
                            smStudent.FormClosed += new FormClosedEventHandler(main_FormClosed);
                            smStudent.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Niepoprawny login lub hasło.",
                            "Błąd logowania",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Warning,
                            MessageBoxDefaultButton.Button1);
                    }
                }
                else
                {
                    MessageBox.Show("Nie wczytano pliku konfiguracyjnego.\nTo sie nie powinno stac :D",
                            "Błąd ładowania konfiguracji",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error,
                            MessageBoxDefaultButton.Button1);
                }
            }
            catch (CryptographicException ex)
            {
                MessageBox.Show("Błąd dekodowania pliku konfiguracyjnego. \nPodane hasło jest niepoprawne lub plik jest uszkodzony.",
                    "Błąd ładowania konfiguracji",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1);
            }


            /*
            TeacherDAO teacherDAO = new TeacherDAO();
            //Teacher teacher = teacherDAO.getById(1);
            //teacherDAO.delete(teacher);

            ClassroomDAO classroomDAO = new ClassroomDAO();
            Classroom classroom = classroomDAO.getById(1);
            EquipmentDAO eqDAO = new EquipmentDAO();
            Equipment eq = eqDAO.getById(1);
            classroom.Equipments.Add(eq);
            classroom.Equipments.Add(eq);
            classroomDAO.update(classroom);
            classroom = classroomDAO.getById(1);
             * 
            foreach (var elem in classroom.Equipments)
            {
                Console.WriteLine("EQ: " + elem.Id + " Name: " + elem.Name);
            }
             * */

            //ScheduleDAO scheduleDAO = new ScheduleDAO();
            //IList<Schedule> schedule = scheduleDAO.getAll();
            //foreach (var elem in schedule)
            //{
                //Console.WriteLine("Schedule: " + elem.Classroom);
            //}

            //Przyklad2
            GroupDAOSchool groupDAO = new GroupDAOSchool();
            //Group group = groupDAO.getById(1);
            //foreach (var elem in group.Subjects)
            //{
            //    Console.WriteLine("Subject id: " + elem.Id + " Login: " + elem.Name);
            //}

            //HourDAO hourDAO = new HourDAO();
            //DayDAO dayDAO = new DayDAO();
            //TimeSpan begin = new TimeSpan(7, 0, 0);
            //TimeSpan end = new TimeSpan(7, 45, 0);
            //Hour hour = new Hour();
            //hour.Day = dayDAO.getById(1);
            //hour.Begin = begin;
            //hour.End = end;
            //Console.WriteLine("Godzina: " + begin.ToString());
            //hourDAO.save(hour);
            //IList<Hour> hours = hourDAO.getAll();
            //foreach (var elem in hours)
            //{
            //    Console.WriteLine("Time: " + elem.Id + " " + elem.Begin + " " + elem.End);
            //}

        
        }

        private void main_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }
    }
}
